/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iscte.iul.dcti.pa.gspas.gui;

import java.awt.Window;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import pt.iscte.iul.dcti.pa.gspas.util.Property;
import pt.iscte.iul.dcti.pa.gspas.util.SwingUtils;

/**
 * 
 * @author Gedsimon Pereira - 58554
 * @contact gspas@iscte.pt
 * 
 */
public class GenericWindow extends JDialog {
	/**
	 *
	 */
	private static final long serialVersionUID = 5879894030485928482L;

	private JPanel panelRight;

	private final Object object;

	private final Object model;

	private List<Property> propertiesArguments;

	private Property property;

	public GenericWindow(Object student, Object objModel) {
		this.object = student;
		this.model = objModel;
		initComponents();
	}

	public GenericWindow(Window modal, Object object, Object model,
			Property property, List<Property> propertiesArguments) {
		super(modal, ModalityType.APPLICATION_MODAL);
		this.object = object;
		this.model = model;
		this.propertiesArguments = propertiesArguments;
		this.property = property;
		initComponents();
	}

	private void initComponents() {
		// Common configuration
		Class<? extends Object> objectClass = object.getClass();
		String name = model.getClass().getSimpleName();

		// setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Add " + name);

		setLocationRelativeTo(null);
		setSize(350, 200);

		JSplitPane basePanel = new JSplitPane();
		basePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(name
				+ " Information"));

		List<Property> properties;

		if (propertiesArguments == null) {
			properties = Property.getProperties(objectClass);
		} else {
			properties = propertiesArguments;
		}
		
		JPanel panelLeft = new JPanel();
		int lineNumber = 0;
		final Map<Property, JComponent> mapProperty = new HashMap<Property, JComponent>();
		for (Property property : properties) {
			JComponent component;
			switch (property.getType()) {
			case BOOLEAN:
				panelLeft.add(SwingUtils.createLabel(property.getLabel()));
				component = SwingUtils.createBoolean("",property.getValue(object, Boolean.class));
				panelLeft.add(component);
				mapProperty.put(property, component);
				lineNumber++;
				break;
			case INT:
				panelLeft.add(SwingUtils.createLabel(property.getLabel()));
				component = SwingUtils.createTxtField(toStringField(property.getValue(object, int.class)));
				panelLeft.add(component);
				mapProperty.put(property, component);
				lineNumber++;
				break;
			case DOUBLE:
				panelLeft.add(SwingUtils.createLabel(property.getLabel()));
				component = SwingUtils.createTxtField(toStringField(property.getValue(object, double.class)));
				panelLeft.add(component);
				mapProperty.put(property, component);
				lineNumber++;
				break;
			case ENUM:

				break;
			case STRING:
				panelLeft.add(SwingUtils.createLabel(property.getLabel()));
				component = SwingUtils.createTxtField(toStringField(property.getValue(object, String.class)));
				panelLeft.add(component);
				mapProperty.put(property, component);
				lineNumber++;
				break;
			default:
				break;
			}
		}
		panelLeft.setLayout(new java.awt.GridLayout(lineNumber, 2));
		
		panelRight = new JPanel();
		panelRight.add(new JLabel(""));
		JButton btnDone = new JButton();
		btnDone.setText("Done");

		btnDone.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				property.callAdd(object, getInformationFromArguments(mapProperty));
				setVisible(false);
			}
		});
		panelRight.add(btnDone);

		basePanel.setLeftComponent(panelLeft);
		basePanel.setRightComponent(panelRight);
		this.add(basePanel);

		this.revalidate();
	}

	private Object[] getInformationFromArguments(
			Map<Property, JComponent> mapProperty) {
		Object[] ret = new Object[propertiesArguments.size()];
		int i = 0;
		for (Property prop : propertiesArguments) {
			if (mapProperty.get(prop) != null) {
				ret[i++] = SwingUtils.getValueFromComponent(prop, mapProperty.get(prop));
			} else {
				ret[i++] = null;
			}
		}
		return ret;
	}

	private String toStringField(Object value) {
		return value != null ? value.toString() : "";
	}
}
